Partial Class pages_Locations_Location : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property LocationId() As Byte?
        Get
            Return CWeb.RequestByteNullable("locationId")
        End Get
    End Property
    Public ReadOnly Property IsEdit() As Boolean
        Get
            Return LocationId.HasValue
        End Get
    End Property
#End Region

#Region "Members"
    Private m_location As CLocation
#End Region

#Region "Data"
    Public ReadOnly Property [Location]() As CLocation
        Get
            If IsNothing(m_location) Then
                If IsEdit Then
                    m_location = CLocation.Cache.GetById(LocationId)
                    If IsNothing(m_location) Then CSitemap.RecordNotFound("Location", LocationId.Value)
                Else
                    m_location = New CLocation
                    'Inserts: set parentId here (if applicable)
                End If
            End If
            Return m_location
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.LocationEdit(Me.Location.LocationId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Locations)
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()

        'Page Title
        If IsEdit Then            
            Me.Title = "Location Details"
        Else
            Me.Title = "Create New Location"
        End If
                
        'Textbox logic
        'Me.Form.DefaultFocus = txtLocationName.Textbox.ClientID;
        Me.Form.DefaultButton = btnSave.UniqueID   'txtLocationName.OnReturnPress(btnSave)

        'Button Text
        btnDelete.Visible = IsEdit
        If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Location"
        If IsEdit Then AddButton(CSitemap.LocationAdd(), "Create a new Location")
    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostback Then Exit Sub

        LoadLocation()
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not Me.IsValid() Then Exit Sub
        SaveLocation()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.Location.Delete()
            'CCache.ClearCache()
        ReturnToList()        
    End Sub
#End Region

#Region "Private - Load/Save"
    Protected Sub LoadLocation()
        With Me.Location
            txtLocationName.Text = .LocationName
        End With
    End Sub
    Protected Sub SaveLocation()
        With Me.Location
            .LocationName = txtLocationName.Text

            .Save()
        End With
            'CCache.ClearCache()
    End Sub
#End Region

'#Region "Private - Bulk Load/Save"
'(Sample Only, Collection property not implemented - use checkboxes/ajax/session etc)
'    Protected Sub LoadLocations()
'        With Me.Locations
'        txtLocationName.Text = IIF(Locations.HaveSameValue("LocationName"),  Locations(0).LocationName, BULKEDIT_NOCHANGE)
'        End With
'    Protected Sub SaveLocations()
'        With Me.Locations
'            If BULKEDIT_NOCHANGE <> txtLocationName.Text Then Locations.SetSameValue("LocationName", txtLocationName.Text)

'            .SaveAll()
'        End With
            'CCache.ClearCache()
'    End Sub
'#End Region

End Class
